package cn.imvc.data.jpa.repository;

import java.io.Serializable;

import javax.persistence.EntityManager;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.support.JpaEntityInformation;
import org.springframework.data.jpa.repository.support.JpaRepositoryFactory;
import org.springframework.data.jpa.repository.support.LockModeRepositoryPostProcessor;
import org.springframework.data.repository.core.RepositoryMetadata;


public class IJpaRepositoryFactory extends JpaRepositoryFactory {

	public IJpaRepositoryFactory(EntityManager entityManager) {
		super(entityManager);
	}
	protected Class<?> getRepositoryBaseClass(RepositoryMetadata metadata) {

		return ISimpleJpaRepository.class;
	}
	@SuppressWarnings({ "unchecked", "rawtypes" })
	protected <T, ID extends Serializable> JpaRepository<?, ?> getTargetRepository(RepositoryMetadata metadata,
			EntityManager entityManager) {

		@SuppressWarnings("unused")
		Class<?> repositoryInterface = metadata.getRepositoryInterface();
		JpaEntityInformation<?, Serializable> entityInformation = getEntityInformation(metadata.getDomainType());

		ISimpleJpaRepository<?, ?> repo =  new ISimpleJpaRepository(entityInformation, entityManager);
		repo.setLockMetadataProvider(LockModeRepositoryPostProcessor.INSTANCE.getLockMetadataProvider());

		return repo;
	}
}
